﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using MediaAutomatorConsole.Configuration;
using MediaAutomatorLib;
using MediaAutomatorLib.ExternalPrograms;
using MediaAutomatorLib.MediaFinder;

namespace MediaAutomatorConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            RecognizersCollection recognizers = MediaAutomatorConfiguration.GetConfig().Recognizers;
            var moviesRecognizers = new List<String>();
            var tvShowsRecognizers = new List<String>();
            foreach (RecognizerElement recognizer in recognizers)
            {
                switch (recognizer.MediaType)
                {
                    case MediaType.Movie:
                        moviesRecognizers.Add(recognizer.Regex);
                        break;
                    case MediaType.TvShow:
                        tvShowsRecognizers.Add(recognizer.Regex);
                        break;
                }
            }

            IgnorersCollection ignorers = MediaAutomatorConfiguration.GetConfig().Ignorers;
            var moviesIgnorers = new List<String>();
            var tvShowsIgnorers = new List<String>();
            foreach (RecognizerElement ignorer in ignorers)
            {
                switch (ignorer.MediaType)
                {
                    case MediaType.Movie:
                        moviesIgnorers.Add(ignorer.Regex);
                        break;
                    case MediaType.TvShow:
                        tvShowsIgnorers.Add(ignorer.Regex);
                        break;
                }
            }


            var config = new MediaAutomatorLib.Configurations.Configuration()
                             {
                                 Downloads = MediaAutomatorConfiguration.GetConfig().Directories.Downloads,
                                 Temporary = MediaAutomatorConfiguration.GetConfig().Directories.Temporary,
                                 MoviesLibrary = MediaAutomatorConfiguration.GetConfig().Directories.MoviesLibrary,
                                 TvShowsLibrary = MediaAutomatorConfiguration.GetConfig().Directories.TvShowsLibrary,
                                 LogsFolderPath = MediaAutomatorConfiguration.GetConfig().Logs,
                                 TheRenamerPath = MediaAutomatorConfiguration.GetConfig().TheRenamer.Path,
                                 MoviesRecognizers = moviesRecognizers,
                                 TvShowsRecognizers = tvShowsRecognizers,
                                 MoviesIgnorers = moviesIgnorers,
                                 TvShowsIgnorers = tvShowsIgnorers,
                                 XbmcHostname = MediaAutomatorConfiguration.GetConfig().Xbmc.Hostname,
                                 XbmcPort = MediaAutomatorConfiguration.GetConfig().Xbmc.Port,
                                 XbmcUsername = MediaAutomatorConfiguration.GetConfig().Xbmc.Username,
                                 XbmcPassword = MediaAutomatorConfiguration.GetConfig().Xbmc.Password,
                                 UTorrentHostname = MediaAutomatorConfiguration.GetConfig().UTorrent.Hostname,
                                 UTorrentPort = MediaAutomatorConfiguration.GetConfig().UTorrent.Port,
                                 UTorrentUsername = MediaAutomatorConfiguration.GetConfig().UTorrent.Username,
                                 UTorrentPassword = MediaAutomatorConfiguration.GetConfig().UTorrent.Password,
                             };

            var mediaAutomator = MediaAutomator.Instance(config);
            if (args.Length > 0)
            {
                string torrentHash = args[0];
                mediaAutomator.RunScan(torrentHash);
            }else
                mediaAutomator.RunScan();




        }


    }
}
